D:\a\tools.proto\tools.proto\compiler\src\gen\rust\message\write.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::compiler::message::Message; |
30 | | use crate::compiler::util::types::TypePathMap; |
31 | | use crate::gen::base::map::{DefaultTypeMapper, TypePathMapper}; |
32 | | use crate::gen::base::message::Templates; |
33 | | use crate::gen::base::message_write::generate; |
34 | | use crate::gen::base::Error; |
35 | | use crate::gen::rust::util::{gen_where_clause, RustUtils}; |
36 | | use crate::gen::template::Template; |
37 | | use crate::gen::{codec::CodecMap, RustParams}; |
38 | | use itertools::Itertools; |
39 | | |
40 | | const TEMPLATE: &[u8] = include_bytes!("write.template"); |
41 | | |
42 | 35 | fn _gen_message_write_impl( |
43 | 35 | msg: &Message, |
44 | 35 | codec_map: &CodecMap, |
45 | 35 | type_path_map: &TypePathMapper<DefaultTypeMapper>, |
46 | 35 | generics: &str, |
47 | 35 | function: &str, |
48 | 35 | ) -> Result<String, Error> { |
49 | 35 | let mut templates = Templates { |
50 | 35 | template: Template::compile(TEMPLATE).unwrap(), |
51 | 35 | codec_map, |
52 | 35 | }; |
53 | 35 | let where_clauses = msg |
54 | 35 | .fields |
55 | 35 | .iter() |
56 | 66 | .map(|field| gen_where_clause(&templates.template, field, type_path_map, function)) |
57 | 35 | .join(""); |
58 | 35 | templates.template.var("where_clauses", where_clauses); |
59 | 35 | if generics.is_empty() { Branch (59:8): [True: 10, False: 25]
Branch (59:8): [Folded - Ignored]
|
60 | 10 | templates.template.var("impl_generics", "").var("generics", "<'_>"); |
61 | 25 | } else { |
62 | 25 | templates.template.var("generics", generics).var("impl_generics", generics); |
63 | 25 | } |
64 | 35 | generate::<RustUtils, _>(templates, msg, type_path_map, function) |
65 | 35 | } |
66 | | |
67 | 28 | pub fn gen_message_write_impl( |
68 | 28 | msg: &Message, |
69 | 28 | codec_map: &CodecMap, |
70 | 28 | type_path_map: &TypePathMap, |
71 | 28 | params: &RustParams, |
72 | 28 | ) -> Result<String, Error> { |
73 | 28 | let type_path_map = TypePathMapper::new(type_path_map, DefaultTypeMapper); |
74 | 28 | let generics = RustUtils::get_generics_for_write(msg, &type_path_map).to_string(); |
75 | 28 | let mut code = _gen_message_write_impl(msg, codec_map, &type_path_map, &generics, "impl")?0 ; |
76 | 48 | if msg.fields.iter().any(28 |v| v.header.is_some())28 { Branch (76:8): [True: 5, False: 23]
Branch (76:8): [Folded - Ignored]
|
77 | 5 | code += &_gen_message_write_impl(msg, codec_map, &type_path_map, &generics, "impl_shape_write")?0 ; |
78 | 23 | } |
79 | 28 | if params.enable_write_async { Branch (79:8): [True: 2, False: 26]
Branch (79:8): [Folded - Ignored]
|
80 | 2 | code += &_gen_message_write_impl(msg, codec_map, &type_path_map, &generics, "impl_async")?0 ; |
81 | 26 | } |
82 | 28 | Ok(code) |
83 | 28 | } |